package action;

import controller.Action;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import model.Curso;
import model.Professor;
import util.ConverteData;
import util.Estados;

public abstract class ManterProfessorAction extends Action {

    @Override
    public void prepararOperacao(HttpServletRequest request, HttpServletResponse response) throws ServletException {
        try {
            String action = request.getParameter("action");
            request.setAttribute("action", action);
            request.setAttribute("cursos", Curso.obterCursos());
            List<Enum> estados = new ArrayList<Enum>();
            estados.addAll(Arrays.asList(Estados.values()));
            request.setAttribute("estados", estados);
            setParametros(request, response);
            RequestDispatcher view = request.getRequestDispatcher("/manterProfessor.jsp");
            view.forward(request, response);
        } catch (ServletException e) {
            throw e;
        } catch (IOException e) {
            throw new ServletException(e);
        } catch (SQLException e) {
            throw new ServletException(e);
        } catch (ClassNotFoundException e) {
            throw new ServletException(e);
        }
    }

    @Override
    public void confirmarOperacao(HttpServletRequest request, HttpServletResponse response) throws ServletException {
        try {
            int matricula = Integer.parseInt(request.getParameter("matricula"));
            String nomeProfessor = request.getParameter("nomeProfessor");
            Date dataNascimento = ConverteData.converte(request.getParameter("dataNascimento"));
            String logradouro = request.getParameter("logradouro");
            int numero = Integer.parseInt(request.getParameter("numero"));
            String complemento = request.getParameter("complemento");
            String bairro = request.getParameter("bairro");
            String cep = request.getParameter("cep");
            String cidade = request.getParameter("cidade");
            String estado = request.getParameter("estado");
            String email = request.getParameter("email");
            Date dataIngresso = ConverteData.converte(request.getParameter("dataIngresso"));
            String titulacao = request.getParameter("titulacao");
            int codCurso = Integer.parseInt(request.getParameter("curso"));
            Professor professor = new Professor(matricula, nomeProfessor, dataNascimento,
                    logradouro, numero, complemento, bairro, cep, cidade, estado, email,
                    dataIngresso, titulacao, Curso.obterCurso(codCurso));
            efetuaPersistencia(professor);
            RequestDispatcher view = request.getRequestDispatcher("FrontController?action=PesquisarProfessor");
            view.forward(request, response);
        } catch (ServletException e) {
            throw e;
        } catch (IOException e) {
            throw new ServletException(e);
        } catch (SQLException e) {
            throw new ServletException(e);
        } catch (ClassNotFoundException e) {
            throw new ServletException(e);
        }
    }

    public abstract void setParametros(HttpServletRequest request, HttpServletResponse response) throws ServletException;

    public abstract void efetuaPersistencia(Professor professor) throws ServletException;

}
